# encoding:utf-8
"""
===============***===============
Auther : Hsir
Date   : 2022 - 11 - 18   17:37
File   : interface_register.py
===============***===============
"""
import json
import flask
from autotest03.comms.dbutils import DBUtils

app = flask.Flask(__name__)



@app.route("/register", methods=['post'])
def register():
    data = flask.request.values.to_dict()
    if 'username' not in data:
        return json.dumps({"code": 1001, "msg": "用户名不能为空!"},
                          ensure_ascii=False)  # 把字典转为jsonge格式（loads（）是json转为字典）
    elif 'password' not in data:
        return json.dumps({"code": 1002, "msg": "密码不能为空!"}, ensure_ascii=False)
    elif 're_password' not in data:
        return json.dumps({"code": 1003, "msg": "确认密码不能为空!"}, ensure_ascii=False)
    elif 'phone' not in data:
        return json.dumps({"code": 1004, "msg": "手机号不能为空!"}, ensure_ascii=False)
    elif 'email' not in data:
        return json.dumps({"code": 1005, "msg": "邮箱不能为空!"}, ensure_ascii=False)

    name = data["username"]
    passwd = data["password"]
    re_passwd = data["re_password"]
    phone = data["phone"]
    email = data["email"]

    if len(name) == 0:
        return json.dumps({"code": 1006, "msg": "用户名不能为空!"}, ensure_ascii=False)
    elif len(passwd) == 0:
        return json.dumps({"code": 1007, "msg": "密码不能为空!"}, ensure_ascii=False)
    elif passwd != re_passwd:
        return json.dumps({"code": 1008, "msg": "两次密码输入不一致!"}, ensure_ascii=False)
    elif not (6 <= len(name) <= 8) or not (6 <= len(passwd) <= 8):
        return json.dumps({"code": 1009, "msg": "用户名和密码必须在6-8位之间!"}, ensure_ascii=False)
    elif len(phone) == 0:
        return json.dumps({"code": 1010, "msg": "手机号不能为空!"}, ensure_ascii=False)
    elif len(phone) != 11 or not phone.isnumeric():
        return json.dumps({"code": 1011, "msg": "手机号码格式错误！!"}, ensure_ascii=False)
    elif len(email) == 0:
        return json.dumps({"code": 1012, "msg": "邮箱不能为空!"}, ensure_ascii=False)
    elif not ('@' in email and '.com' in email):
        return json.dumps({"code": 1013, "msg": "邮箱格式不正确!"}, ensure_ascii=False)
    else:
        db = DBUtils()
        name_count = db.find_count("select * from tb_user where name = %s", (name,))
        phone_count = db.find_count("select * from tb_user where phone =%s", (phone,))
        email_count = db.find_count("select * from tb_user where email =%s", (email,))
        if name_count >= 1:
            db.close()
            return json.dumps({"code": 1014, "msg": "用户名已存在!"}, ensure_ascii=False)
        elif phone_count >= 1:
            db.close()
            return json.dumps({"code": 1015, "msg": "手机号已存在!"}, ensure_ascii=False)
        elif email_count >= 1:
            db.close()
            return json.dumps({"code": 1016, "msg": "该邮箱已存在!"}, ensure_ascii=False)
        else:
            # 注册成功的场景，需要把注册用户信息落库
            count = db.cud("insert into businessdb.tb_user(name,passwd,email,phone) values(%s,%s,%s,%s)",
                           (name, passwd, email, phone))
            db.close()
            if count == 1:
                return json.dumps({"code": 6666, "msg": "恭喜注册成功!"}, ensure_ascii=False)
            else:
                return json.dumps({"code": 0000, "msg": "注册失败，环境异常，请联系管理员!"}, ensure_ascii=False)


if __name__ == '__main__':
    app.run(port=6969)
